package phoenix.mob_android.page_objects;

import java.util.ArrayList;

import org.openqa.selenium.WebElement;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseMobAndroidExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.mob_android.locators.base.ProfilePageLocatorsBase;

public class ProfilePage extends PageBaseMobAndroidExt
{
	public ProfilePage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	/**
	 * Gets web elements by site
	 * @return SettingsPageLocatorsBase instance
	 */
	public ProfilePageLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.mob_android.locators.flirt.ProfilePageLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.mob_android.locators.wildbuddies.ProfilePageLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.mob_android.locators.cheeckylovers.ProfilePageLocators();
			}
			default:
			{
				return null;
			}
		}
	}

	public void isOnProfilePage()
	{
		md().waitForPresence(elements().blockMyProfile, "My profile page doesn't appear");
	}

	public void isAboutDescriptionPresent()
	{
		md().waitForVisibility(elements().blockUserInfoDescription, "Users info description doesn't appear");
	}

	public ProfilePage openAboutProfileInfo()
	{
		md().wait(2);
		md().waitForVisibility(elements().btnAbout, "Button about doesn't appear");
		md().click(elements().btnAbout);

		return this;
	}

	public ProfilePage checkForBottomMenu()
	{
		md().waitForVisibility(elements().blockBottomMenu, "Bottom menu doesn't appear on the profile page");
		if (!md().isElementDisplayed(elements().btnChatBottomMenu))
		{
			log().errorAndScreen("Button chat bottom menu doesn't appear");
		}
		if (!md().isElementDisplayed(elements().btnWinkBottomMenu))
		{
			log().errorAndScreen("Button wink bottom menu doesn't appear");
		}
		if (!md().isElementDisplayed(elements().btnFavoritesBottomMenu))
		{
			log().errorAndScreen("Button favorites bottom menu doesn't appear");
		}
		if (!md().isElementDisplayed(elements().btnAbout))
		{
			log().errorAndScreen("Button about bottom menu doesn't appear");
		}

		return this;
	}

	public void addUserToBlackList()
	{
		md().waitForVisibility(elements().btnBlockMenu, "Button block doesn't appear on the profile page");
		md().click(elements().btnBlockMenu);
		md().click(elements().btnBlock);
	}

	public void addUserToFavourite()
	{
		md().waitForPresence(elements().btnFavoritesBottomMenu, "Button favorites bottom menu doesn't appear");
		md().click(elements().btnFavoritesBottomMenu);
	}

	public void openPrivateChat()
	{
		md().waitForPresence(elements().btnChatBottomMenu, "Button chat bottom menu doesn't appear");
		md().click(elements().btnChatBottomMenu);
	}

	public String getUserScreenname()
	{
		return manager.string().getSubstringBefore(md().getText(elements().blockUserScreenname), ",");
	}

	public ProfilePage clickUpgradeButton()
	{
		md().waitForPresence(elements().btnUpgrade, "Upgrade button doesn't appear");
		md().click(elements().btnUpgrade);

		return this;
	}

	public void setDescriptionWithEditButton(String p_description)
	{
		md().waitForVisibility(elements().btnProfileDescription, "Button profile description doesn't appear");
		md().click(elements().btnProfileDescription);
		md().wait(2);
		md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
		md().click(elements().btnEdit);
		if (!md().isElementPresent(elements().inputFieldProfileDescription))
		{
			md().click(elements().btnProfileDescription);
			md().wait(2);
			md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
			md().click(elements().btnEdit);
		}
		md().typeAndClear(elements().inputFieldProfileDescription, p_description);
	}

	public void setDescriptionWithoutEditButton(String p_description)
	{
		md().wait(3);
		md().waitForVisibility(elements().btnProfileDescription, "Button profile description doesn't appear");
		WebElement p_btnProfDescrip = md().getWebElement(elements().btnProfileDescription);
		md().tap(1, p_btnProfDescrip, 1);
		md().waitForVisibility(elements().inputFieldProfileDescription, "Input field profile description doesn't appear");
		md().typeAndClear(elements().inputFieldProfileDescription, p_description);
		md().hideKeyboardApp();
	}

	private void chooseValueOfDescription(int p_value)
	{
		manager.wd().driver().findElements(elements().selectElements).get(p_value).click();
	}

	public ProfilePage setHeightWithEditButton()
	{
		md().waitForVisibility(elements().btnProfileHeight, "Button height doesn't appear");
		md().click(elements().btnProfileHeight);
		md().wait(2);
		md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
		md().click(elements().btnEdit);
		if (!md().isElementPresent(elements().btnSubmitProfileInfo))
		{
			md().click(elements().btnProfileHeight);
			md().wait(2);
			md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
			md().click(elements().btnEdit);
		}
		// Value 4' 5" (1.35m)
		this.chooseValueOfDescription(6);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setHeightWithoutEditButton()
	{
		md().waitForVisibility(elements().btnProfileHeight, "Button height doesn't appear");
		md().click(elements().btnProfileHeight);
		// Value 4' 5" (1.35m)
		this.chooseValueOfDescription(6);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setBodyWithEditButton()
	{
		md().waitForVisibility(elements().btnProfileBody, "Button body doesn't appear");
		md().click(elements().btnProfileBody);
		md().wait(2);
		md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
		md().click(elements().btnEdit);
		if (!md().isElementPresent(elements().btnSubmitProfileInfo))
		{
			md().click(elements().btnProfileBody);
			md().wait(2);
			md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
			md().click(elements().btnEdit);
		}
		// Value Athletic
		this.chooseValueOfDescription(3);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setBodyWithoutEditButton()
	{
		md().waitForVisibility(elements().btnProfileBody, "Button height doesn't appear");
		md().click(elements().btnProfileBody);
		// Value Athletic
		this.chooseValueOfDescription(3);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setOrientationWithEditButton()
	{
		md().waitForVisibility(elements().btnProfileOrientation, "Button orientation doesn't appear");
		md().click(elements().btnProfileOrientation);
		md().wait(2);
		md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
		md().click(elements().btnEdit);
		if (!md().isElementPresent(elements().btnSubmitProfileInfo))
		{
			md().click(elements().btnProfileOrientation);
			md().wait(2);
			md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
			md().click(elements().btnEdit);
		}
		// Value Bisexual
		this.chooseValueOfDescription(2);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setOrientationWithoutEditButton()
	{
		md().waitForVisibility(elements().btnProfileOrientation, "Button height doesn't appear");
		md().click(elements().btnProfileOrientation);
		// Value Bisexual
		this.chooseValueOfDescription(2);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setMaritalStatusWithEditButton()
	{
		md().waitForVisibility(elements().btnProfileMaritalStatus, "Button marital status doesn't appear");
		md().click(elements().btnProfileMaritalStatus);
		md().wait(2);
		md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
		md().click(elements().btnEdit);
		if (!md().isElementPresent(elements().btnSubmitProfileInfo))
		{
			md().click(elements().btnProfileMaritalStatus);
			md().wait(2);
			md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
			md().click(elements().btnEdit);
		}
		// Value Single
		this.chooseValueOfDescription(1);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setMaritalStatusWithoutEditButton()
	{
		md().waitForVisibility(elements().btnProfileMaritalStatus, "Button height doesn't appear");
		md().click(elements().btnProfileMaritalStatus);
		// Value Single
		this.chooseValueOfDescription(1);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setSmokeWithEditButton()
	{
		md().waitForVisibility(elements().btnProfileSmoke, "Button smoke doesn't appear");
		md().click(elements().btnProfileSmoke);
		md().wait(2);
		md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
		md().click(elements().btnEdit);
		if (!md().isElementPresent(elements().btnSubmitProfileInfo))
		{
			md().click(elements().btnProfileSmoke);
			md().wait(2);
			md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
			md().click(elements().btnEdit);
		}
		// Value Rather not say
		this.chooseValueOfDescription(4);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setSmokeWithoutEditButton()
	{
		md().waitForVisibility(elements().btnProfileSmoke, "Button height doesn't appear");
		md().click(elements().btnProfileSmoke);
		// Value Rather not say
		this.chooseValueOfDescription(4);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setDrinkWithEditButton()
	{
		md().waitForVisibility(elements().btnProfileDrink, "Button drink doesn't appear");
		md().click(elements().btnProfileDrink);
		md().wait(2);
		md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
		md().click(elements().btnEdit);
		if (!md().isElementPresent(elements().btnSubmitProfileInfo))
		{
			md().click(elements().btnProfileDrink);
			md().wait(2);
			md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
			md().click(elements().btnEdit);
		}
		// Value No
		this.chooseValueOfDescription(1);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setDrinkWithoutEditButton()
	{
		md().waitForVisibility(elements().btnProfileDrink, "Button height doesn't appear");
		md().click(elements().btnProfileDrink);
		// Value No
		this.chooseValueOfDescription(1);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setChildrenWithEditButton()
	{
		md().waitForVisibility(elements().btnProfileChildren, "Button children doesn't appear");
		md().click(elements().btnProfileChildren);
		md().wait(2);
		md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
		md().click(elements().btnEdit);
		if (!md().isElementPresent(elements().btnSubmitProfileInfo))
		{
			md().click(elements().btnProfileChildren);
			md().wait(2);
			md().waitForVisibility(elements().btnEdit, "Button edit doesn't appear");
			md().click(elements().btnEdit);
		}
		// Value I have children
		this.chooseValueOfDescription(3);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ProfilePage setChildrenWithoutEditButton()
	{
		md().waitForVisibility(elements().btnProfileChildren, "Button height doesn't appear");
		md().click(elements().btnProfileChildren);
		// Value I have children
		this.chooseValueOfDescription(3);
		md().click(elements().btnSubmitProfileInfo);

		return this;
	}

	public ArrayList<String> getDescription()
	{
		ArrayList<String> list_personalInfo = new ArrayList<String>(8);

		md().wait(5);
		list_personalInfo.add(md().getText(elements().btnProfileDescription));
		list_personalInfo.add(md().getText(elements().btnProfileHeight));
		list_personalInfo.add(md().getText(elements().btnProfileBody));
		list_personalInfo.add(md().getText(elements().btnProfileOrientation));
		list_personalInfo.add(md().getText(elements().btnProfileMaritalStatus));
		list_personalInfo.add(md().getText(elements().btnProfileSmoke));
		list_personalInfo.add(md().getText(elements().btnProfileDrink));
		list_personalInfo.add(md().getText(elements().btnProfileChildren));

		return list_personalInfo;
	}
}
